package com.s666.demo.utils;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;


/** */

/**
 * <p>
 * RSA公钥/私钥/签名工具包
 *
 */
public class RSAUtils {

	/** */
	/**
	 * 加密算法RSA
	 */
	public static final String KEY_ALGORITHM = "RSA";

	/** */
	/**
	 * 签名算法
	 */
	public static final String SIGNATURE_ALGORITHM = "SHA1WithRSA";

	/** */
	/**
	 * 获取公钥的key
	 */
	private static final String PUBLIC_KEY = "RSAPublicKey";

	/** */
	/**
	 * 获取私钥的key
	 */
	private static final String PRIVATE_KEY = "RSAPrivateKey";

	/** */
	/**
	 * RSA最大加密明文大小
	 */
	private static final int MAX_ENCRYPT_BLOCK = 117;

	/** */
	/**
	 * RSA最大解密密文大小
	 */
	private static final int MAX_DECRYPT_BLOCK = 128;

	/** */
	/**
	 * <p>
	 * 生成密钥对(公钥和私钥)
	 * </p>
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> genKeyPair() throws Exception {
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
		keyPairGen.initialize(1024);
		KeyPair keyPair = keyPairGen.generateKeyPair();
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
		RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
		Map<String, Object> keyMap = new HashMap<String, Object>(2);
		keyMap.put(PUBLIC_KEY, publicKey);
		keyMap.put(PRIVATE_KEY, privateKey);
		return keyMap;
	}

	/** */
	/**
	 * <p>
	 * 用私钥对信息生成数字签名
	 * </p>
	 * 
	 * @param data
	 *            已加密数据
	 * @param privateKey
	 *            私钥(BASE64编码)
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String sign(byte[] data, String privateKey) throws Exception {
		byte[] keyBytes = Base64Util.decode(privateKey);
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
		Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
		signature.initSign(privateK);
		signature.update(data);
		return Base64Util.encode(signature.sign());
	}

	/** */
	/**
	 * <p>
	 * 校验数字签名
	 * </p>
	 * 
	 * @param data
	 *            已加密数据
	 * @param publicKey
	 *            公钥(BASE64编码)
	 * @param sign
	 *            数字签名
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	public static boolean verify(byte[] data, String publicKey, String sign)
			throws Exception {
		byte[] keyBytes = Base64Util.decode(publicKey);
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		PublicKey publicK = keyFactory.generatePublic(keySpec);
		Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
		signature.initVerify(publicK);
		signature.update(data);
		return signature.verify(Base64Util.decode(sign));
	}

	/** */
	/**
	 * <P>
	 * 私钥解密
	 * </p>
	 * 
	 * @param encryptedData
	 *            已加密数据
	 * @param privateKey
	 *            私钥(BASE64编码)
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
		byte[] keyBytes = Base64Util.decode(privateKey);
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, privateK);
		int inputLen = encryptedData.length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offSet = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段解密
		while (inputLen - offSet > 0) {
			if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
				cache = cipher
						.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
			} else {
				cache = cipher
						.doFinal(encryptedData, offSet, inputLen - offSet);
			}
			out.write(cache, 0, cache.length);
			i++;
			offSet = i * MAX_DECRYPT_BLOCK;
		}
		byte[] decryptedData = out.toByteArray();
		out.close();
		return decryptedData;
	}

	/** */
	/**
	 * <p>
	 * 公钥解密
	 * </p>
	 * 
	 * @param encryptedData
	 *            已加密数据
	 * @param publicKey
	 *            公钥(BASE64编码)
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
		byte[] keyBytes = Base64Util.decode(publicKey);
		X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key publicK = keyFactory.generatePublic(x509KeySpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.DECRYPT_MODE, publicK);
		int inputLen = encryptedData.length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offSet = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段解密
		while (inputLen - offSet > 0) {
			if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
				cache = cipher
						.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
			} else {
				cache = cipher
						.doFinal(encryptedData, offSet, inputLen - offSet);
			}
			out.write(cache, 0, cache.length);
			i++;
			offSet = i * MAX_DECRYPT_BLOCK;
		}
		byte[] decryptedData = out.toByteArray();
		out.close();
		return decryptedData;
	}

	/** */
	/**
	 * <p>
	 * 公钥加密
	 * </p>
	 * 
	 * @param data
	 *            源数据
	 * @param publicKey
	 *            公钥(BASE64编码)
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
		byte[] keyBytes = Base64Util.decode(publicKey);
		X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key publicK = keyFactory.generatePublic(x509KeySpec);
		// 对数据加密
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, publicK);
		int inputLen = data.length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offSet = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段加密
		while (inputLen - offSet > 0) {
			if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
				cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(data, offSet, inputLen - offSet);
			}
			out.write(cache, 0, cache.length);
			i++;
			offSet = i * MAX_ENCRYPT_BLOCK;
		}
		byte[] encryptedData = out.toByteArray();
		out.close();
		return encryptedData;
	}

	/** */
	/**
	 * <p>
	 * 私钥加密
	 * </p>
	 * 
	 * @param data
	 *            源数据
	 * @param privateKey
	 *            私钥(BASE64编码)
	 * @return
	 * @throws Exception
	 */
	public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
		byte[] keyBytes = Base64Util.decode(privateKey);
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
		Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		cipher.init(Cipher.ENCRYPT_MODE, privateK);
		int inputLen = data.length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offSet = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段加密
		while (inputLen - offSet > 0) {
			if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
				cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(data, offSet, inputLen - offSet);
			}
			out.write(cache, 0, cache.length);
			i++;
			offSet = i * MAX_ENCRYPT_BLOCK;
		}
		byte[] encryptedData = out.toByteArray();
		out.close();
		return encryptedData;
	}

	/** */
	/**
	 * <p>
	 * 获取私钥
	 * </p>
	 * 
	 * @param keyMap
	 *            密钥对
	 * @return
	 * @throws Exception
	 */
	public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
		Key key = (Key) keyMap.get(PRIVATE_KEY);
		return Base64Util.encode(key.getEncoded());
	}

	/** */
	/**
	 * <p>
	 * 获取公钥
	 * </p>
	 * 
	 * @param keyMap
	 *            密钥对
	 * @return
	 * @throws Exception
	 */
	public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
		Key key = (Key) keyMap.get(PUBLIC_KEY);
		return Base64Util.encode(key.getEncoded());
	}


	public static void main(String[] args) throws Exception {


	/*	Map<String, Object> map = genKeyPair();
		String private_key = getPrivateKey(map);
		String pubKey = getPublicKey(map);

		Map<String, Object> server = genKeyPair();
		String server_private_key = getPrivateKey(server);
		String server_pubKey = getPublicKey(server);

		System.out.println("private_key:   ");
		System.out.println(private_key);
		System.out.println();
		System.out.println();
		System.out.println("pubKey:   ");
		System.out.println(pubKey);

		System.out.println();
		System.out.println();

		System.out.println("server_private_key:   ");
		System.out.println(server_private_key);
		System.out.println();
		System.out.println();
		System.out.println("server_pubKey:   ");
		System.out.println(server_pubKey);*/



		//String private_key = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKFHOMfQO+bc8Wy5lg8Bbe+YgI5QH/WMCN6Yr4vsSR10d6eYUnOR+8+BhLpyKVbM849piBwmwfV9lWj/Hg1U+60F/GxORgKB+SzeTEgXVCfqXSSH2rWlwBE7OTXEFK12C5LinXGa/22LPNY7jBLBCG9SQozeJzRe69Ihuue69NyDAgMBAAECgYALRLGn7FmzGK7ZnOHqLHxk6C/bQafp/R3Fh7+END8riq6tjAv+vS1t4yvF5yISSYGe/I2hAeg9YcflrSWJYu2unajLAhtfr9HhKCJZk5l2YN41GzAGm1Qd47BKZtRi44BhpQgqW7+n0uQLITFLWZjNCN1vS2gaD4SkZYoTzGCSyQJBANuJbOcUrmTei4ENgVGONzQ+eHkrl7sZoItoiSSbTtXWd+d9Fzqab4NDkYa7YiKmqnxbLmetOdaBzmSPvnBOhoUCQQC8EKzVXWUnlTyP+xa5FH3Nn8OEMawRvrEaXVUQss1ieHc0f3xRL9koWfyvM2Rd99hPXUTPZyrp/Tk7b9pE3tlnAkEAwNQoj6AevgKrTiNqnxPncUAd2XBsya5s0YZ0T28LA9BpaS76pELaB9XlkQ4t2HnD7Y65Z99DmiJSAwovH74ZcQJAJgZtG7sFj+pR037eSk/FXAUYoCr28qOO5ZjHcVflxSo8WAYK2dOF4e3H9Ji8i29ociuWlSTz/Vmw776e8FvqswJBAMHj7TRgBojUAK4mJkbLMcWMo+bA2ijMdZdEBmeQSIQS7YnhvDI3KTEENJnkqHkFKMLiy4M7P+Nw1Ybm2UppCI0=";
		//String pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChRzjH0Dvm3PFsuZYPAW3vmICOUB/1jAjemK+L7EkddHenmFJzkfvPgYS6cilWzPOPaYgcJsH1fZVo/x4NVPutBfxsTkYCgfks3kxIF1Qn6l0kh9q1pcAROzk1xBStdguS4p1xmv9tizzWO4wSwQhvUkKM3ic0XuvSIbrnuvTcgwIDAQAB";
		//String server_pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCO9/WSFp4wuPJt0HSqXsjP05jCGqmmVT60BnJgVVO0vEuE6g81Y+aTHiYH3d9WjIBCJVXAbiHO5nihbaYVpsqdSDFzOyH8O+fzC3Tf6ZpZ2LxU07jVsbnHm2SMNcma2Xu8WVHjGxpLkTPg8wB9I32tZnLZHPst3VSON6n+1GJB4wIDAQAB";
		//String server_private_key = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAI739ZIWnjC48m3QdKpeyM/TmMIaqaZVPrQGcmBVU7S8S4TqDzVj5pMeJgfd31aMgEIlVcBuIc7meKFtphWmyp1IMXM7Ifw75/MLdN/pmlnYvFTTuNWxucebZIw1yZrZe7xZUeMbGkuRM+DzAH0jfa1mctkc+y3dVI43qf7UYkHjAgMBAAECgYA8nOGvKwmsLG0To6IirQbEHbiQ6o7sZw2rc2RAdUyEvqPh2P3Tq+QQkWKcSTOr1Ot3n66IY+jPeCTCg+M7ILiba3IqGNlXknyfEjs413FmjDm+SGZDn4Jm6yoxUL1IhiIEOIYtPIhKZmPO/6XBS+bUCQWES/cWxmkIRHoXhR2ZiQJBAOLOpSacPpQFZ5YQ2yZgboFLDg6xA2k4dzbtEfhvlYEwDLQoZ7TWIL+gFsXqJvik9fut144swBJFblgt6jCgRt0CQQChXs99XoO/969RuzmF1XHtmma69tmJUpaAtJPkFbC7h8h/nOmKpVy49OXGoAG8sALnz78H+uogThyWl7GegT+/AkEA38MB2orRjlWascnlfAItY6+5qbLn65pCu1S+ovzrYe4WynMGhbmvy35UmWP+mNaevHPxMxphUGVhjOuXneuynQJAcNqGj23vA+zrXmRqaS+J+ppVqAGExyXL/XzMROHqdGwUizKonX7iJrOWkl9Kd7Q8g/Pk7GbNIASgt6yk2m4XrwJAen1Ti1WNkGx3x0XJ0y+sve5Z1Pr20JFbGqcpmuX/690LrKrPJMY1NtXCL3nf1FLlomOWbcew+DUyDEAlw3mcPw==";
		
		String server_pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDaDFq9EtbLkac7jVjCD2fvjispw/TA5E8Cv9fvUdO0xC4grEx+dDTdY9AlLgIaI+H4ODgZd3Nv+IVLZSSACptS5OYJ1+4Ul5+fJvNTS99PMsQHuiUdJU+HX03gVt8rFpGXVRHv2rsC5YQtw2pLpvmGD3oA1nrOlqVGfNiyapETKwIDAQAB";
		String server_private_key = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBANoMWr0S1suRpzuNWMIPZ++OKynD9MDkTwK/1+9R07TELiCsTH50NN1j0CUuAhoj4fg4OBl3c2/4hUtlJIAKm1Lk5gnX7hSXn58m81NL308yxAe6JR0lT4dfTeBW3ysWkZdVEe/auwLlhC3Dakum+YYPegDWes6WpUZ82LJqkRMrAgMBAAECgYEA043DB8jcb7vMdkIISJgr7keQZ0BBGlkos3Ewb3iDn9HrMzYYLWRP2jC6RShjT7taxbq96y9y58NGiEUEadpgoPvUjdJuHV/YnPxLqtg6Rv+uyv4dLQOGA8vIVSu/9aMfozq8LfnCxSiX57AVJ7SblQLl129p9EZInn6xykZuu4kCQQD+QprgS48bvsYpzFiQA1v6ojQzc4RtfVztLld7RLJr7mLfp4qyn77FwmbH2N7tiY/dZIRoZ/ECeKmiAiibb2XNAkEA24pQ5HJZjynVY9C7SnWY9EC4Sz9iKVdIoV1wsVtgbdaSlWCRjos0S8QcfTvGL3ctkqN8SbyMDJduJIbXdrjk1wJBAP4+odnZFF/9EvBm6sQO5+GmYqZ0Q56sdcihx0528ibk+C+GvDVDcaMUcmwyGzDRlm8efRxdm20KNS3QFMTgzp0CQQDZ59LcvOdJ4zLrrO2JPWoA3hBOl+39rKXpjI5ja5gtzXGXFM/E1USmA1RntSHEUR2J0/10TtIPWWq4lKJeM6znAkBXamAUXICBH7bZ8HLio5MHRFHT393dSx7wUWOqRLj8obn3UrhWKx3AGxHSZ69s12LwymKhXVxCYF+4lmoxXPAv";

		//-------client--------
		String partnerParam = "method=credit.single.realtime.query&partner=10001111111111111111&data={\"param1\":\"paramV1\"}";
		byte[] encodeData = encryptByPublicKey(partnerParam.getBytes(), server_pubKey);

		System.out.println( Base64Util.byteArrayToBase64(encodeData) );

		// php 公钥加密的 java私钥解密
		byte[] enstrkey = Base64Util.decode("defBKil4wEOg+fYsEuLM+sZfFoLSGYk/WL9BaKlUiHcY1Kd1h2Em2CbKpAqX+pVQP1edDJYjOOCunxJOygoVKp7JUaZumI5PMUS3Jhl8lGpR94PWnyB++sNohM5GjfJs036Nip+CBpwPsKDZnJvu+JuTkNq5dBER2S8JZxzl3smiA2Ip1U7Ac51eFGi3rEtFJO5aJqjc1GFYNjKAPHbEYuuJA6PD14w02BENGXugGncr87ZEzpPYL1aICKuIm7sSwLp+z0VOulmclA0688Z+g0iJeyRr8Tsh0LgwY7h5RhwYHhAx2qrvPqJEF1dHT/Azr345yw0VjwzsgOUkaoBFBaJBQ23E4JNp/Mihrlkk9i1NvIQHU9PDH0XoKxIGo4LXXfpI26bQJ9rOR9nzRoKCRo7e/330ACb/73Ag0xpHuZJro/wWN2YIshJrC77KcB2m7AA6RFH6SGUjkajuZ4F7oBAn1clLG9Q4xtKean2jsszGGN0zvD/nakicUbM1UTNKyS3VkWWh1ZhwQGyVvShUO4Flda3IhtyNzSWSGjbRrZUfnHUFcBS/dSGPfAISV+ypcueV9cuSY7y/2i/dly9ZjHFoFuHlIafr79m7VEsKT1G2BLPDrs7nkwxsx09A7+gggWLl2aQ1978p2DxWEwHHJJbjgl8MyLTjcAmvNBLYSB3AHTmzv5+BAsv6XXObbjUgoM0mqwhR6Luso1PRelU3XDRGFgANzuj4b5MHSl1p4j1uKMbNeSZ/U/MHo7trQdjZVv4sK/tuyIn343CI4haFWLugKBB/1fKXHx14eZVc7kb/KhlpjwFQj3mv82rjJlsJPkTdv4lT/rPJ6YByQ8Ynng==");
		byte[] data2 = decryptByPrivateKey(enstrkey, server_private_key);
		System.out.println( "php 公钥加密的 java私钥解密______________" );
		System.out.println( new String(data2, "utf-8") );
		
		// php 私钥加密的 java公钥解密
		byte[] enstr = Base64Util.decode("upA25DhR1lFXEHoxZkpfvW/i4ejDNFdYwRrrFz7vmc089lt8LPaER9MCBFnH+PTxnOBOC4icNaak3Aq4AnvzpAJPXEG1nK9tJcdS1vTt5RH8k+PjmC11So+VZORLYjpaSi6M1feE2OQM3D4nOD4lynXahff/r19K2dl8YVG3e7XI+LK7QjH7JXYViS+uTUxARxdb7qGJ6Zi/8/S96AxGzRU4XwOju6RoNBSlcfx87k0/tau1SUk9cR+RMXsFjfrQ9J+MhahNq3vv4dL1dmR5Br2aa08yUhaiINX1T3b7VWT2Ov6VXM7ZsBOA6s4sjMGM5ZxIJT/XaQtQWafVBU9t+9N611H9gyQ94pCGJi5h24mdfwOcJP9qa8A2iufg2PbvJokvmsG2XRMgJmPbA+cKBv28T8AsaZX78HgkGRWnEZji0oOQ1X606KvB1yJ4MHxwaIXdwgjAoZ+ABn/ldLcY3aYohal4/eKiI4FIuKESDr8i4xpJTV2Vq7KTvO+J5P0dKDJ0478CtQmj+t4Kt1xZuaEwFi4ApO71W8x5J29cJtGpITIgfyIFMZhEB1fMWQ1U0g9jNaI5rCvflwuLZfolkwRrNSluefIf86ivxybui05TkgLdg/7RE5tCdFLaZ7dMNg4enCPtzgF/obVlNOL/jKTKnejFMTvS3vFwuE/x5SU/fqUy73mnv9x5ZF8J7esWjIUKIxTHDpP3yEGv/QHbKZWAsh4QcTBeNQQc16dScC0YdbCLDsYQy7c8P0/fjCcBZ1OJ87XatzTk+ZInUobZQ/lWKbeDT8xoXwXkOnqv8cwTwkFWmHt4rQsJoR969yQ9gJoWgrB29cN2W8i6p2rxMw==");
		byte[] data = decryptByPublicKey(enstr, server_pubKey);
		System.out.println( "php 私钥加密的 java公钥解密______________" );
		System.out.println( new String(data, "utf-8") );
		

		// java私钥加密
		String prid = "java私钥加密java私java私钥加密钥加密jjava私钥加密java私java私钥加密钥加密java私钥加密java私钥加密java私钥加密java私钥加密java私钥加密ava私钥加密java私钥加密java私钥加密java私钥加密java私钥加密";
		byte[] data3 = encryptByPrivateKey(prid.getBytes(), server_private_key);
		System.out.println( "java私钥加密______________" );
		System.out.println( Base64Util.encode(data3) );

		// java公钥加密
		String prid1 = "java私钥加密java私java私钥加密钥加密jjava私钥加密java私java私钥加密钥加密java私钥加密java私钥加密java私钥加密java私钥加密java私钥加密ava私钥加密java私钥加密java私钥加密java私钥加密java私钥加密";
		byte[] data4 = encryptByPublicKey(prid1.getBytes(), server_pubKey);
		System.out.println( "java公钥加密______________" );
		System.out.println( Base64Util.encode(data4) );
		
		// java签名
		String s1 = "javjajjavjajavjava签名ajavjajjavjajavjava签名a签名va签javjajjavjajavjava签名ajavjajjavjajavjava签名a签名va签名a签名avjava签名a签名va签名a签名签名va签名a签名avjava签名a签名va签名a签名名a签名avjava签名a签名va签名a签名签名va签名a签名avjava签名a签名va签名a签名";
		String sign1 = sign(s1.getBytes(), server_private_key);
		System.out.println( "java签名______________" );
		System.out.println( sign1 );
		
		
		// java验签
		String datas="Ta24G4/10qojESEZRLnvbY82RMJIWuhDkEvFrq2x/Z7sN3XC5hm+1RmifV7dVonLyJ2b8b1kzk84x4iNSwkubeqrUAm0m9ylV8Y+qiubU+THPJv0QBhxE/GXx8e1G7aAt4gqDuWgCk7w2lzrsAQqOFh9LBhrHNsNQvjyKyas7wg=";
		boolean sign_ok = verify(s1.getBytes(), server_pubKey,datas );
		System.out.println( "java验签______________" );
		System.out.println( sign_ok );
		
		
		//byte[] res = HttpUtil.post("http://localhost:8888/visit-gateway/gateway", arr);

		/*System.out.println("res byte:"+res);
		System.out.println("res byte str:" + new String(res, "utf-8"));

		String str = new String(RSAUtils.decryptByPrivateKey(res, private_key), "utf-8");
		System.out.print("res:" + str);
		String[] paraArr = str.split("&");
		for(String para : paraArr) {
			System.out.println(para);

		}

		String serverSign = str.split("sign=")[1];
		System.out.println("sign:" + serverSign);

		String other = str.split("&sign=")[0];
		System.out.println("verfy sign:"+verify(other.getBytes(), server_pubKey, serverSign));


		//-------server--------
//		partnerParam = "paraA=aa&paraB=bb&paraC=cc";
//		String clientP = new String(decryptByPrivateKey(arr, server_private_key), "utf-8");
//		System.out.println(verify(partnerParam.getBytes(), pubKey, sign));
		//
		*/

	}

}